﻿using Approval.API.Applcation.Command.HCTRelationCommand;
using Approval.Domain;
using Approval.Domain.Common;
using Approval.Infrastructure.Impl;
using Approval.Infrastructure.Interface;
using AutoMapper;

using MediatR;
using Newtonsoft.Json;
using PublicCommon;

namespace Approval.API.Applcation.CommandHandler.HCTRelationCommandHandler
{
    public class HCTRelationAppprovalCommandHandler : IRequestHandler<HCTRelationAppprovalCommand, Audit>
    {
        private readonly IHCTRelationRepository hCTRelationRepository;
        private readonly IMapper mapper;
        private readonly Cross_services_Helper cross_services_helper;

        public HCTRelationAppprovalCommandHandler(IHCTRelationRepository hCTRelationRepository, IMapper mapper, Cross_services_Helper cross_services_helper)
        {
            this.hCTRelationRepository = hCTRelationRepository;
            this.mapper = mapper;
            this.cross_services_helper = cross_services_helper;
        }

        public async Task<Audit> Handle(HCTRelationAppprovalCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var info = hCTRelationRepository.All().Result.Where(x => x.HCTId == request.HCTId && x.ApprovalId == request.UserId).FirstOrDefault();
                //审批时间
                info.CreateDate = DateTime.Now;
                //审批备注
                info.ApprovalDesc = request.ApprovalDesc;
                //同意
                if (request.Identifier == 1)
                {
                    info.ApprovalState = 2;
                    await hCTRelationRepository.Update(info);

                    var hctapprocal = new HCTRelation()
                    {
                        HCTId = request.HCTId,
                        ApprovalId = request.TransferId,
                        ApprovalState = 1,
                        ApprovalName = request.TransferName,
                        ApprovalDesc = null
                    };

                    var res = await hCTRelationRepository.Create(hctapprocal);
                    var audit = new Audit()
                    {
                        CreateDate = DateTime.Now,
                        CreateBy = request.TransferName,
                        CreateById = request.TransferId,
                        AuditState = 1,
                        IsDel = false
                    };
                    return audit;
                }
                //拒绝
                if (request.Identifier == 2)
                {
                    info.ApprovalState = 3;
                    await hCTRelationRepository.Update(info);
                    var audit = new Audit()
                    {
                        CreateDate = DateTime.Now,
                        CreateBy = request.UserName,
                        CreateById = request.UserId,
                        AuditState = 1,
                        IsDel = false
                    };

                    var Hct = await cross_services_helper.Get($"http://10.31.60.9:8000/crout/api/ReadContract/InfoContract?ContractNumber={request.HCTId}");
                    var Hcts = JsonConvert.DeserializeObject<HCTDto>(Hct);
                    var HctList = new HLZState();
                    HctList.id = Hcts.ContractId;
                    HctList.auditState = 3;
                    Hcts.Approver = "小明";
                    var res = await cross_services_helper.Post("http://10.31.60.9:8000/csrout/api/Contract/AlterState", HctList);
                    return audit;
                }
                //通过并终止转批
                if (request.Identifier == 3)
                {
                    info.ApprovalState = 2;
                    await hCTRelationRepository.Update(info);
                    var audit = new Audit()
                    {
                        CreateDate = DateTime.Now,
                        CreateBy = request.UserName,
                        CreateById = request.UserId,
                        AuditState = 1,
                        IsDel = false
                    };
                    var Hct = await cross_services_helper.Get($"http://10.31.60.9:8000/crout/api/ReadContract/InfoContract?ContractNumber={request.HCTId}");
                    var Hcts = JsonConvert.DeserializeObject<HCTDto>(Hct);
                    var HctList = new HLZState();
                    HctList.id = Hcts.ContractId;
                    HctList.auditState = 2;
                    Hcts.Approver = "小明";
                    var res = await cross_services_helper.Post("http://10.31.60.9:8000/csrout/api/Contract/AlterState", HctList);
                    return audit;
                }


                return info;
            }
            catch (Exception)
            {
                return null;
                throw;
            }


        }
    }
}
